/*
cd /projects/hsieh_project/proj_201809/code_2_202011/
qstata mkt_d_sect_quintile.do &
*/ 

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/mkt_d_sect_quintile_log, replace text

clear all
cd /projects/

di "$S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/mkt_d_sect_quintile/"
noi capture mkdir ${dir_outf}
noi capture mkdir ${dir_outf}/data/

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

do "/projects/hsieh_project/code_0_general/f_rounding.do"

global ds_ind "${dir_data}/ind_sum_all"

global lvmkt = "est msa1983 fips zipcode"


tempfile ds_temp
global ds_temp = "`ds_temp'"

/*--------------------------------------------------------------------------------
Author: Adarsh Kumar
Objective: 
1. Identify top and bottom quintile by chg in ln(mkt/firm) between 1977-2013
2. Find their SV average for every 5 years. 
*/

*Call on mkt_d_ind_0 to get change in log markets:
do ${dir_do}/mkt_d_ind_0.do "geo"

*Identify Top & Bottom Quintile of change in log(mkt/firm for 1977-2013)
foreach mkt in $lvmkt {
	do ${dir_do}/mkt_d_ind_0.do "geo"
	keep ch_ind ln_mu_mkt_`mkt'_d
	egen rank_ln_mu_mkt_`mkt'_d = rank(-ln_mu_mkt_`mkt'_d), track
	*Generate quintiles: 1 = highest; 5 = lowest. Creating equally sized quintiles
	local quintile_size = round(_N / 5)
	gen quintile_77_13 = 1 if rank_ln_mu_mkt_`mkt'_d <= `quintile_size' 
	replace quintile_77_13 = 5 if rank_ln_mu_mkt_`mkt'_d >= _N - `quintile_size' + 1
	
	tab quintile_77_13 
	
	keep if quintile_77_13 == 1 | quintile == 5
	
	save ${dir_outf}/data/quintile_ch_ind_list_`mkt', replace
	saveold ${dir_outf}/data/quintile_ch_ind_list_`mkt'_v12, replace v(12)
}

*Calculate SV average every 5 years for each both quintiles
clear

foreach mkt in $lvmkt{
	clear
	save ${ds_temp}_a, replace emptyok
	foreach iyear in 1982 1987 1992 1997 2002 2007 2013 {
		global year1 = 1977
		global year2 = `iyear'
		
		clear
		qui do ${dir_do}/mkt_d_ind_0.do "geo"
		keep ch_ind ln_mu_mkt_`mkt'_d
		
		merge 1:1 ch_ind using ${dir_outf}/data/quintile_ch_ind_list_`mkt', keepusing(quintile_77_13)
		keep if _merge == 3
		drop _merge
		keep if quintile_77_13 == 1 | quintile_77_13 == 5
		tab quintile_77_13
		
		*Merge SV weights
		gen year1 = ${year1}
		gen year2 = ${year2}
		merge 1:1 year1 year2 ch_ind using ${dir_out}/sv_weights/sv_weights_5years, keepusing(w_num)
		keep if _merge == 3
		drop _merge
		
		collapse (mean) ln_mu_mkt_`mkt'_d [aw = w_num], by(quintile year1 year2)
		rounding_4dig "ln_mu_mkt_`mkt'_d"
		
		append using ${ds_temp}_a
		save ${ds_temp}_a, replace
		}
	
	sort quintile year2
	use ${ds_temp}_a, clear
	save "${dir_outf}/`mkt'_ln_chg_5year_SV_mean", replace
	export excel using "${dir_outf}/`mkt'_ln_chg_5year_SV_mean.xlsx", firstrow(var) keepcellfmt replace
	
	
}


